<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item>渠道商列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-form ref="form" :model="searchForm" label-width="80px" :inline="true" size="small" class="admin_from">
                <el-form-item label="" :class="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'maxWidth':''">
                    <el-input v-model="searchForm.phone" placeholder="请输入手机号"
                    ></el-input>
                </el-form-item>
                <el-form-item label="" :class="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'maxWidth':''">
                    <el-input v-model="searchForm.username" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item label="" :class="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'maxWidth':''">
                    <el-select v-model="searchForm.status" placeholder="请选择状态" clearable
                               :class="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'maxWidth':''">
                        <el-option label="禁用" value="0"></el-option>
                        <el-option label="启用" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="small" @click="search">查询</el-button>
                    <el-button type="primary" size="small" @click="addRecharge">添加渠道商</el-button>
                </el-form-item>
            </el-form>
            <el-table
                    size="small"
                    :data="tableData"
                    border
                    style="width: 100%">

                <el-table-column
                        type="index"
                        label="#">
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="站点名称">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="渠道名称">
                </el-table-column>
                <el-table-column
                        prop="address"
                        label="用户信息">
                    <template slot-scope="scope">
                        <div class="img">
                            <div>
                                <p>{{scope.row.id}}</p>
                                <p>{{scope.row.username}}</p>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="phone"
                        label="手机号">
                </el-table-column>
                <el-table-column
                        prop="weixin"
                        label="代理微信">
                </el-table-column>
                <el-table-column
                        prop="qq"
                        label="代理QQ">
                </el-table-column>
                <el-table-column
                        prop="lv"
                        label="返额比例">
                </el-table-column>
                <el-table-column
                        prop="onums"
                        label="距扣量数">
                </el-table-column>
                <el-table-column
                        prop="desc"
                        label="扣量比例">
                </el-table-column>
                <el-table-column
                        width="150"
                        label="收款信息">
                    <template slot-scope="scope">
                        <p v-if="scope.row.receiptmodel==1"><span>银行卡</span></p>
                        <p v-if="scope.row.receiptmodel==1"><span>{{scope.row.bank}}</span></p>
                        <p v-if="scope.row.receiptmodel==1"><span>{{scope.row.bankname}}</span></p>
                        <p v-if="scope.row.receiptmodel==1"><span>{{scope.row.bankno}}</span></p>
                        <p v-if="scope.row.receiptmodel==2"><span>支付宝</span></p>
                        <p v-if="scope.row.receiptmodel==2"><span>{{scope.row.realname}}</span></p>
                        <p v-if="scope.row.receiptmodel==2"><span>{{scope.row.account}}</span></p>
                        <p v-if="scope.row.receiptmodel==3"><span>微信</span></p>
                        <p v-if="scope.row.receiptmodel==3"><span>{{scope.row.realname}}</span></p>
                        <p v-if="scope.row.receiptmodel==3"><span>{{scope.row.account}}</span></p>
                    </template>
                </el-table-column>
                <el-table-column
                        label="类型">
                    <template slot-scope="scope">
                        <span v-if="scope.row.type==1">渠道商</span>
                        <span v-if="scope.row.type==2">代理商</span>
                    </template>
                </el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        <span v-if="scope.row.status==0">禁用</span>
                        <span v-if="scope.row.status==1">启用</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="创建时间">
                    <template slot-scope="scope">
                        {{scope.row.created_at}}
                    </template>
                </el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        width="150px">
                    <template slot-scope="scope">
                        <el-button type="warning" plain size="small" @click="edit(scope.row.id)">修改</el-button>
                        <el-button type="danger" plain size="small" @click="remove(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="el-pagination">
                <el-pagination
                        background
                        @size-change="changeSize"
                        @current-change="changeCurrent"
                        :current-page="params.pagenum"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size="params.pagesize"
                        :layout="this.$store.state.paginationLayout"
                        :total="params.totals">
                </el-pagination>
            </div>

        </el-card>
        <!--            新增或编辑-->
        <el-dialog :title="fromTitle" :width="this.$store.state.dialogWidth" :visible.sync="visible">
            <el-row :gutter="20">
                <el-col :span="this.$store.state.elColNum">
                    <el-form ref="form" :label-width="this.$store.state.labelWidth" size="small">
                        <el-form-item label="站点名称">
                            <el-input v-model="dataForm.title"></el-input>
                        </el-form-item>
                        <el-form-item label="渠道名称">
                            <el-input v-model="dataForm.name"></el-input>
                        </el-form-item>
                        <el-form-item label="防封域名">
                            <el-input v-model="dataForm.url"></el-input>
                        </el-form-item>
                        <el-form-item label="用户名">
                            <el-input v-model="dataForm.username"></el-input>
                        </el-form-item>
                        <el-form-item label="密码">
                            <el-input v-model="dataForm.tpassword"></el-input>
                        </el-form-item>
                        <el-form-item label="手机号">
                            <el-input v-model="dataForm.phone"></el-input>
                        </el-form-item>
                        <el-form-item label="代理QQ">
                            <el-input v-model="dataForm.qq"></el-input>
                        </el-form-item>
                        <el-form-item label="代理微信">
                            <el-input v-model="dataForm.weixin"></el-input>
                        </el-form-item>
                        <el-form-item label="App ID">
                            <el-input v-model="dataForm.appid"></el-input>
                        </el-form-item>
                        <el-form-item label="App Secret">
                            <el-input v-model="dataForm.appsecret"></el-input>
                        </el-form-item>
                        <el-form-item label="返额比例">
                            <el-input v-model="dataForm.lv"></el-input>
                            <span class="input_hint">单位：%，填写 0 到 100 之间的数字，设置比例不能超过100%</span>
                        </el-form-item>
                        <el-form-item label="距扣量数">
                            <el-input v-model="dataForm.onums"></el-input>
                            <span class="input_hint">订单数量达到设定的距扣量数笔进行扣量</span>
                        </el-form-item>
                        <el-form-item label="扣量比例">
                            <el-input v-model="dataForm.desc"></el-input>
                            <span class="input_hint">每多少笔订单扣除一笔订单</span>
                        </el-form-item>
                        <el-form-item label="收款方式">
                            <el-select v-model="dataForm.receiptmodel" placeholder="选择收款方式" style="width: 100%">
                                <el-option label="银行卡" :value="1"></el-option>
                                <el-option label="支付宝" :value="2"></el-option>
                                <el-option label="微信" :value="3"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="结算银行" v-if="dataForm.receiptmodel==1">
                            <el-input v-model="dataForm.bank"></el-input>
                        </el-form-item>
                        <el-form-item label="结算收款人" v-if="dataForm.receiptmodel==1">
                            <el-input v-model="dataForm.bankname"></el-input>
                        </el-form-item>
                        <el-form-item label="结算银行卡号" v-if="dataForm.receiptmodel==1">
                            <el-input v-model="dataForm.bankno"></el-input>
                        </el-form-item>
                        <el-form-item label="账号" v-if="dataForm.receiptmodel==2||dataForm.receiptmodel==3">
                            <el-input v-model="dataForm.account"></el-input>
                        </el-form-item>
                        <el-form-item label="真实姓名" v-if="dataForm.receiptmodel==2||dataForm.receiptmodel==3">
                            <el-input v-model="dataForm.realname"></el-input>
                        </el-form-item>
<!--                        <el-form-item label="渠道公众号二维码">-->
<!--                            <el-upload-->
<!--                                    class="avatar-uploader"-->
<!--                                    action="upDataUrl"-->
<!--                                    :show-file-list="false"-->
<!--                                    :http-request="myUpload"-->
<!--                                    :before-upload="beforeAvatarUpload">-->
<!--                                <img v-if="dataForm.qrcode" :src="dataForm.qrcode" class="avatar">-->
<!--                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
<!--                            </el-upload>-->
<!--                        </el-form-item>-->
                        <el-form-item label="状态">
                            <el-select v-model="dataForm.status" placeholder="状态" style="width: 100%">
                                <el-option label="禁用" :value="0"></el-option>
                                <el-option label="启用" :value="1"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="visible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="dialogAffirm" size="small">确 定</el-button></span>
        </el-dialog>
    </div>
</template>

<script>
  import {uploadimg} from '../../utils/common'
  import {formatDate} from '../../utils/common'

  export default {
    name: "User",
    data() {
      return {
        params: {
          pagenum: 1,
          pagesize: 10,
          totals: 0
        },
        form: {
          subscribe: '',
          charge: '',
          nickname: '',
        },
        dataForm: {
          type:'1',
          url:'',
          title: '',
          name: '',
          username: '',
          tpassword: '',
          phone: '',
          weixin: '',
          lv: '',
          qq: '',
          onums: '',
          desc: '',
          receiptmodel: '',
          bank: '',
          bankname: '',
          bankno: '',
          account: '',
          realname: '',
          status: '',
          appsecret:'',
          appid:'',
          ips:'',
          paymodel:'',
          qrcode:'',
        },
        imgData: {},
        domain: '',
        tableData: [],
        fromTitle: '',
        visible: false,
        imageUrl: '',
        id: '',
        searchForm: {
          phone: '',
          username: '',
          status: '',
          type: '',
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        let data = {
          page: this.params.pagenum,
          page_size: this.params.pagesize,
          phone: this.searchForm.phone,
          username: this.searchForm.username,
          status: this.searchForm.status,
          type: '1',
        };
        this.$axios.post("admin/mch/index", data).then((res) => {
          this.params.totals = parseInt(res.data.count);
          this.tableData = res.data.list;
          this.tableData.map((num) => {
            num.login_time = formatDate(num.login_time)
          })
        });
      },
      // 每页显示多少条
      changeSize(val) {
        this.params.pagesize = val;
        this.getList()
      },
      // 当前显示的页数
      changeCurrent(val) {
        this.params.pagenum = val;
        this.getList()
      },
      // 添加用户
      addRecharge() {
        this.fromTitle = '新增';
        this.id = '';
        this.dataForm = {
          type:'1',
          url:'',
          title: '',
          name: '',
          username: '',
          tpassword: '',
          phone: '',
          weixin: '',
          lv: '',
          qq: '',
          onums: '',
          desc: '',
          receiptmodel: '',
          bank: '',
          bankname: '',
          bankno: '',
          account: '',
          realname: '',
          status: '',
          appsecret:'',
          appid:'',
          ips:'',
          paymodel:'',
          qrcode:'',
        };
        this.visible = true;
      },
      // 编辑用户
      edit(id) {
        this.id = id;
        this.fromTitle = '编辑';
        this.$axios.get('admin/mch/' + id, {}).then((res) => {
          let data = res.data;
          this.dataForm = data;
          this.visible = true;
        });
      },
      // 确认提交添加或编辑用户
      dialogAffirm() {
        if (this.id) {
          this.$axios.put('admin/mch/' + this.id, this.dataForm).then((res) => {
            this.$message.success('成功');
            this.getList();
            this.visible = false;
          });
        } else {
          // 添加
          this.$axios.post('/admin/mch', this.dataForm).then((res) => {
            this.$message.success('成功');
            this.getList();
            this.visible = false;
          });
        }
      },
      remove(id) {
        this.$confirm('确定要进行操作, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.del('admin/mch/' + id, '').then((res) => {
            this.$message.success('删除成功!');
            this.getList();
          });
        }).catch(() => {

        });
      },
      search() {
        this.params.pagenum = 1;
        this.getList();
      },
      beforeAvatarUpload(file) {
        var suffix = file.name.substring(file.name.lastIndexOf('.') + 1);
        let arr = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'pdf', 'JPG', 'JPEG', 'PNG', 'GIF', 'BMP', 'PDF']
        if (arr.indexOf(suffix) === -1) {
          this.$message.error('上传图片格式有误');
          return false
        }
      },
      // 上传文件
      myUpload(content) {
        uploadimg(content).then(res => {
          this.dataForm.qrcode = res;
        })
      },
    }
  }
</script>

<style scoped>
    .img {
        display: flex;
        align-items: center;
    }

    .img > img {
        margin-right: 10px;
    }
</style>